package com.apply.repository.spec;

import com.apply.entity.ApplyInfo;
import com.apply.entity.ApplyInfo_;
import com.apply.entity.ExamInfo;
import com.apply.entity.SysUser;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.jpa.domain.Specification;

import javax.persistence.criteria.*;

/**
 * @program: online-regs
 * @description:
 * @author: tianp
 * @create: 2018-05-03 22:43
 **/
public class ApplyInfoRepositorySpec {

    public static Specification<ApplyInfo> getSpec(final ApplyInfo applyInfo) {
        return new Specification<ApplyInfo>() {
            @Override
            public Predicate toPredicate(Root<ApplyInfo> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                Predicate p1 = null;
//                StringUtils.isNotEmpty()
                if (StringUtils.isNotEmpty(applyInfo.getCartNum())) {
                    Predicate p2 = cb.equal(root.get(ApplyInfo_.cartNum), applyInfo.getCartNum());
                    if (p1 != null) {
                        p1 = cb.and(p1, p2);
                    } else {
                        p1 = p2;
                    }
                }
                if (StringUtils.isNotEmpty(applyInfo.getApplyId())) {
                    Predicate p2 = cb.equal(root.get(ApplyInfo_.applyId), applyInfo.getApplyId());
                    if (p1 != null) {
                        p1 = cb.and(p1, p2);
                    } else {
                        p1 = p2;
                    }
                }
                if (StringUtils.isNotEmpty(applyInfo.getSex())) {
                    Predicate p2 = cb.equal(root.get(ApplyInfo_.sex), applyInfo.getSex());
                    if (p1 != null) {
                        p1 = cb.and(p1, p2);
                    } else {
                        p1 = p2;
                    }
                }
                if (StringUtils.isNotEmpty(applyInfo.getRealName())) {
                    Predicate p2 = cb.like(root.get(ApplyInfo_.realName), "%" + applyInfo.getRealName() + "%");
                    if (p1 != null) {
                        p1 = cb.and(p1, p2);
                    } else {
                        p1 = p2;
                    }
                }
                if (null != applyInfo.getSysUser()) {
                    Join<ApplyInfo, SysUser> inner = root.join("sysUser", JoinType.INNER);
                    if (null != applyInfo.getSysUser().getId()) {
                        Predicate p2 = cb.equal(inner.get("id"), applyInfo.getSysUser().getId());
                        if (p1 != null) {
                            p1 = cb.and(p1, p2);
                        } else {
                            p1 = p2;
                        }
                    }
                }
                if (null != applyInfo.getExamInfo()) {
                    Join<ApplyInfo, ExamInfo> examInfo = root.join("examInfo", JoinType.INNER);
                    if (null != applyInfo.getExamInfo().getId()) {
                        Predicate p2 = cb.equal(examInfo.get("id"), applyInfo.getExamInfo().getId());
                        if (p1 != null) {
                            p1 = cb.and(p1, p2);
                        } else {
                            p1 = p2;
                        }
                    }
                    if (StringUtils.isNotEmpty(applyInfo.getExamInfo().getExamName())) {
                        Predicate p2 = cb.like(examInfo.get("examName"), "%" + applyInfo.getExamInfo().getExamName() + "%");
                        if (p1 != null) {
                            p1 = cb.and(p1, p2);
                        } else {
                            p1 = p2;
                        }
                    }
                }
                return p1;
            }
        };
    }
}
